<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProductsReviews
 *
 * @author Eugin
 */
class DatabaseObject_ProductsReviews extends DatabaseObject {

    public function __construct($db) {
        parent::__construct($db, 'products_reviews', 'id');

        $this->add('productId');
        $this->add('userId');
        $this->add('author');
        $this->add('text');
        $this->add('rating');
        $this->add('status');
        $this->add('date_added');
    }

    public static function GetReviews($db, $options = array()) {
        // инициализируем настройки
        $defaults = array(
            'where' => '',
            'offset' => 0,
            'limit' => 0,
            'order' => 'pr.id'
        );

        foreach ($defaults as $k => $v) {
            $options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
        }

        $select = self::_GetBaseQuery($db, $options);

        // поля для выборки
        $select->from(null, 'pr.*');

        // зададим настройки
        if ($options['limit'] > 0)
            $select->limit($options['limit'], $options['offset']);
        if ($options['where'])
            $select->where($options['where']);

        $select->order($options['order']);

        // получим данные из базы
        $data = $db->fetchAll($select);

        // сделаем из полученных записей объекты класса
        $cats = self::BuildMultiple($db, __CLASS__, $data);
        $cat_ids = array_keys($cats);

        if (count($cat_ids) == 0)
            return array();

        return $cats;
    }

    public static function GetProductCountReviews($db, $productId) {
        $select = self::_GetBaseQuery($db, array());
        $select->from(null, 'count(*)');
        $select->where('productId = ?', $productId);
        return $db->fetchOne($select);
    }
    
        public static function GetProductReviews($db, $productId) {
        $select = self::_GetBaseQuery($db, array());
        $select->from(null, '*');
        $select->where('productId = ?', $productId);
        $data = $db->fetchAll($select);
        $cats = self::BuildMultiple($db, __CLASS__, $data);
        $cat_ids = array_keys($cats);

        if (count($cat_ids) == 0)
            return array();
        return $cats;
    }

    public static function GetCountReviews($db) {
        $select = self::_GetBaseQuery($db, array());
        $select->from(null, 'count(*)');

        return $db->fetchOne($select);
    }

    private static function _GetBaseQuery($db, $options) {
        // инициализируем параметры
        $defaults = array('id' => array());

        foreach ($defaults as $k => $v) {
            $options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
        }

        // создадим запрос
        $select = $db->select();
        $select->from(array('pr' => 'products_reviews'), array());

        if (isset($options['where'])) {
            $select->where($options['where']);
        }
        if (isset($options['limit'])) {
            $select->limit($options['offset'] . ',' . $options['limit']);
        }

        // отфильтруем записи по id (если задано)
        if (count($options['id']) > 0) {
            $select->where('u.id in (?)', $options['id']);
        }

        return $select;
    }

     public static function DeleteProductsReviews($db, $id) {
         $query = sprintf("delete from %s where productId = %d", 'products_reviews', $id);

        $db->query($query);

        return true;
    }
}
